/**
 * Created by IntelliJ IDEA.
 * User: tobias
 * Date: 5/5/11
 * Time: 4:56 PM
 * To change this template use File | Settings | File Templates.
 */

import edu.udo.cs.wvtool.config.WVTConfiguration;
import edu.udo.cs.wvtool.config.WVTConfigurationFact;
import edu.udo.cs.wvtool.main.WVTDocumentInfo;
import edu.udo.cs.wvtool.main.WVTFileInputList;
import edu.udo.cs.wvtool.main.WVTWordVector;
import edu.udo.cs.wvtool.main.WVTool;
import edu.udo.cs.wvtool.util.WVToolException;
import edu.udo.cs.wvtool.wordlist.WVTWordList;
import net.sf.javaml.core.DenseInstance;

import java.io.*;
import java.util.HashMap;

import static net.sf.javaml.tools.data.FileHandler.loadDataset;

public class BaseModule  {

    private WVTFileInputList filelist;
    private HashMap<Integer, String> classes = new HashMap<Integer,String>();
    private WVTool tool = new WVTool(false);
    private DataSet data = new DataSet(classes);
    private WVTConfiguration config = new WVTConfiguration();

    public void parseDocuments(String directory) throws Exception{
        File dir = new File(directory);
        if(!dir.isDirectory()) {
            throw new Exception("not a directory");
        }
        File[] subdirs = dir.listFiles();
        filelist = new WVTFileInputList(subdirs.length);
        for(int i = 0; i < subdirs.length; i++) {
            classes.put(i, subdirs[i].toString());
            filelist.addEntry(
                    new WVTDocumentInfo(subdirs[i].toString(), "txt", "", "english", i));
        }
    }


    public WVTWordList createWordList() throws WVToolException {
        return tool.createWordList(filelist, config);
    }
    public DataSet createVectors(WVTWordList wordlist, int size) throws Exception {
        DataSet data = createVectors(wordlist);
        data.setFeatureSetSize(size);
        return data;

    }

    public DenseInstance createVector(DataSet data, String file) throws IOException, WVToolException {
        return createVector(data.getWordList(), file);
    }
    public DenseInstance createVector(WVTWordList wordlist, String file) throws WVToolException, IOException {
        byte[] buffer = new byte[(int) new File(file).length()];
           BufferedInputStream f = null;
           try {
               f = new BufferedInputStream(new FileInputStream(file));
               f.read(buffer);
           } finally {
               if (f != null) try { f.close(); } catch (IOException ignored) { }
           }
        String input = new String(buffer);

        WVTWordVector wv = tool.createVector(input,  wordlist);

        DenseInstance ins = new DenseInstance(wv.getValues(), wv.getDocumentInfo().getClassValue());
        return ins;
    }

    public DataSet createVectors(WVTWordList wordlist) throws Exception {
        config.setConfigurationRule(
			WVTConfiguration.STEP_OUTPUT,
			new WVTConfigurationFact(data));

        tool.createVectors(filelist, config, wordlist);
        data.addWordList(wordlist);
        return data;
    }
    public DataSet loadDataSet(File file, File wordfile) throws IOException {
        WVTWordList wordlist = new WVTWordList(new FileReader(wordfile));
        DataSet data = new DataSet(loadDataset(file, 0, "\t"));
        data.addWordList(wordlist);
        return data;
    }
}
